package com.bayss.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.bayss.core.DokoServerException;
import com.bayss.core.DokoServerExceptionFactor;
import com.bayss.core.constant.ResultMsg;
import com.bayss.core.entity.station.Station;
import com.bayss.core.service.address.AddressService;
import com.bayss.core.service.station.StationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * 配送站
 *
 * @author yangjian
 */
@Slf4j
@RestController
public class StationController extends ApiBaseController{

    private final StationService stationService;

    private final AddressService addressService;

    @Autowired
    public StationController(StationService stationService, AddressService addressService) {
        this.stationService = stationService;
        this.addressService = addressService;
    }

    /**
     * 计算配送费
     * @return
     */
    @RequestMapping("/station/cost_price.json")
    public ResultMsg costPrice(@RequestParam(name = "stationId") Long stationId,
                               @RequestParam(name = "addressId") Long addressId) {
        return ResultMsg.success(stationService.costPrice(addressId, stationId));
    }

    /**
     * 计算送达时间
     * @return
     */
    @RequestMapping("/station/cost_time.json")
    public ResultMsg costTime(@RequestParam(name = "stationId") Long stationId,
                              @RequestParam(name = "addressId", required = false, defaultValue = "0") Long addressId,
                              @RequestParam(name = "isReserve") Integer isReserve,
                              @RequestParam(name = "orderType") Integer orderType
                              ) {
        Station station = stationService.get(stationId);
        JSONObject jsonObject = new JSONObject();
        if (isReserve == 0) {
            Map<String, Long> costMap = stationService.costTime(addressId, stationId);
            long m = costMap.get("sum_time");
            long doTime = costMap.get("do_time");
            Date date = new Date();
            long timeMillis = date.getTime() + m * 60 * 1000;
            if (orderType == 3) {
                timeMillis = date.getTime();
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            jsonObject.put("start", timeMillis);
            jsonObject.put("revc_time", sdf.format(new Date(date.getTime()+doTime * 60 * 1000)));
            String str = sdf.format(date).split(" ")[0] + " " + station.getOrderEndTime();
            try {
                if (sdf.parse(str).getTime()<date.getTime()){
                    throw new DokoServerException(DokoServerExceptionFactor.ORDER_END, "门店已经休息");
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else {
            jsonObject.put("start", station.getOpenStartTime());
        }

        jsonObject.put("end", station.getOrderEndTime());
        jsonObject.put("interview", 15);

        return ResultMsg.success(jsonObject);



//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Map<String, Integer> costMap = stationService.costTime(addressId, stationId);
//        int m = costMap.get("sum_time");
//        long timeMillis = System.currentTimeMillis() + m * 60 * 1000;
//
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("at_once", sdf.format(new Date(timeMillis)));
//
//        long index = (timeMillis-timeMillis%3600000);
//        if (timeMillis - index > 30*60*1000) {
//            index += 60*60*1000;
//        } else {
//            index += 30*60*1000;
//        }
//
//        JSONArray jsonArray = new JSONArray();
//        Map<String, List<String>> map = new LinkedHashMap<>();
//        for (int i=0; i<6; i++) {
//            String strTime = sdf.format(new Date(index));
//            String key = strTime.split(" ")[0];
//            String val = strTime.split(" ")[1];
//            if (map.containsKey(key)) {
//                List<String> list = map.get(key);
//                list.add(val);
//            } else {
//                List<String> list = new ArrayList<>();
//                list.add(val);
//                map.put(key, list);
//            }
//            index += 1000*60*30;
//        }
//        map.forEach((k, v) -> {
//            JSONObject jObj = new JSONObject();
//            jObj.put("date", k);
//            jObj.put("times", v);
//            jsonArray.add(jObj);
//        });
//
//        jsonObject.put("other", jsonArray);
//        return ResultMsg.success(jsonObject);
    }

    /**
     * 获取配送
     * @return
     */
    @RequestMapping("/station/get.json")
    public ResultMsg get(@RequestParam(name = "addrId", required = false, defaultValue = "0") Long addrId,
                         @RequestParam(name = "isExistAddrId") Integer isExistAddrId,
                         @RequestParam(name = "longitude", required = false, defaultValue = "") String longitude,
                         @RequestParam(name = "latitude", required = false, defaultValue = "") String latitude) {

        log.info(String.format("station get param is %s,%s,%s,%s", addrId, isExistAddrId, longitude, latitude));
        System.out.println(String.format("station get param is %s,%s,%s,%s", addrId, isExistAddrId, longitude, latitude));
        return ResultMsg.success( stationService.getByWeb(addrId, isExistAddrId, longitude, latitude));
    }






















}
